package bieudoindex.backing;

import bieudoindex.bieudoindex;

import bieudoindex.resource.bieudoindexBundle;

import common.Utils;

import java.util.Locale;

import javax.faces.context.FacesContext;

import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletMode;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequest;
import javax.portlet.WindowState;

import oracle.adf.model.binding.DCIteratorBinding;

public class bieudoindexBacking {
    private String mPortletTitle;
    private String sanhoseid;
    private String sanhnxid;
    private String langid;
    private String tieude;
    private String TensanHose;
    private String TensanHNX;
    private Object HienthiHose;
    private Object HienthiHNX;
    private int minHoseVnIndex = 0;
    private int minHoseKLGD = 0;
    private int minHnxVnIndex = 0;
    private int minHnxKLGD = 0;
    
    public bieudoindexBacking(){
          
    }

    public String getPortletTitle() {
        if (mPortletTitle == null) {
            PortletRequest request =
                (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
            PortletPreferences preferences = request.getPreferences();
            mPortletTitle =
                    preferences.getValue(bieudoindex.PORTLETTITLE_KEY, getResource(bieudoindexBundle.PORTLETTITLE));
        }
        return mPortletTitle;
    }

    public String getPortletTitleLabel() {
        String resource = getResource(bieudoindexBundle.PORTLETTITLE);
        return resource;
    }

    public void setPortletTitle(String portletTitle) {
        mPortletTitle = portletTitle;
    }

    public String applyAction() throws Exception {
        PortletRequest request =
            (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        PortletPreferences preferences = request.getPreferences();
        preferences.setValues(bieudoindex.PORTLETTITLE_KEY,
                              mPortletTitle.split(","));
        preferences.store();
        return null;
    }

    public String getApplyLabel() {
        String resource = getResource(bieudoindexBundle.APPLY_LABEL);
        return resource;
    }

    public String okAction() throws Exception {
        applyAction();
        ActionResponse response =
            (ActionResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
        response.setPortletMode(PortletMode.VIEW);
        response.setWindowState(WindowState.NORMAL);
        return null;
    }

    public String getOkLabel() {
        String resource = getResource(bieudoindexBundle.OK_LABEL);
        return resource;
    }

    private String getResource(String resourceKey) {
        PortletRequest request =
            (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        PortletConfig config =
            (PortletConfig)request.getAttribute("javax.portlet.config");
        return config.getResourceBundle(request.getLocale()).getString(resourceKey);
    }

    private Locale getPortalLocale() {
        Locale portalLocale = new Locale("vi", "VN");
        String language = "";
        try {

            PortletRequest request =
                (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
            if (request.getParameter("ngonnguid") != null) {
                language = request.getParameter("ngonnguid");
                portalLocale = new Locale(language);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return portalLocale;
    }

    private String Tieude() {
        String tieudebieudo = "";
        try {
            PortletRequest request =
                (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
            if (request.getParameter("tieude") != null)
                tieudebieudo = request.getParameter("tieude").toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return tieudebieudo;
    }

    public void setLangid(String langid) {
        this.langid = langid;
    }

    public String getLangid() {
        langid = getPortalLocale().getLanguage().toString();
        return langid;
    }

    public void setSanhoseid(String sanhoseid) {
        this.sanhoseid = sanhoseid;
    }

    public String getSanhoseid() {
        sanhoseid = "2";
        return sanhoseid;
    }

    public void setSanhnxid(String sanhnxid) {
        this.sanhnxid = sanhnxid;
    }

    public String getSanhnxid() {
        sanhnxid = "1";
        return sanhnxid;
    }

    public void setTieude(String tieude) {
        this.tieude = tieude;
    }

    public String getTieude() {
        tieude = Tieude();
        return tieude;
    }

    public void setTensanHNX(String TensanHNX) {
        this.TensanHNX = TensanHNX;
    }

    public String getTensanHNX() {
        TensanHNX = "HNX";
        DCIteratorBinding IteratorHNX =
            Utils.getIteratorBinding("VnHnxIndexView1Iterator");
        IteratorHNX.getViewObject().clearCache();
        if (IteratorHNX.getAllRowsInRange().length > 0) {
            if (IteratorHNX.getCurrentRow().getAttribute("TenSan") != null) {
                TensanHNX =
                        IteratorHNX.getCurrentRow().getAttribute("TenSan").toString();
            }
        }
        return TensanHNX;
    }

    public void setTensanHose(String TensanHose) {
        this.TensanHose = TensanHose;
    }

    public String getTensanHose() {
        TensanHose = "HOSE";
        DCIteratorBinding IteratorHose =
            Utils.getIteratorBinding("VnHastcIndexView1Iterator");
        IteratorHose.getViewObject().clearCache();
        if (IteratorHose.getAllRowsInRange().length > 0) {
            if (IteratorHose.getCurrentRow().getAttribute("TenSan") != null) {
                TensanHose =
                        IteratorHose.getCurrentRow().getAttribute("TenSan").toString();
            }
        }
        return TensanHose;
    }

    public void setHienthiHose(Object HienthiHose) {
        this.HienthiHose = HienthiHose;
    }

    public Object getHienthiHose() {
        DCIteratorBinding IteratorHnx =
            Utils.getIteratorBinding("VnHastcIndexView1Iterator");
        IteratorHnx.getViewObject().clearCache();
        if (IteratorHnx.getCurrentRow().getAttribute("HienThi") != null) {
            HienthiHose =
                    IteratorHnx.getCurrentRow().getAttribute("HienThi").toString();
        }
        return HienthiHose;
    }

    public void setHienthiHNX(Object HienthiHNX) {
        this.HienthiHNX = HienthiHNX;
    }

    public Object getHienthiHNX() {
        DCIteratorBinding IteratorHNX =
            Utils.getIteratorBinding("VnHnxIndexView1Iterator");
        IteratorHNX.getViewObject().clearCache();
        if (IteratorHNX.getCurrentRow().getAttribute("HienThi") != null) {
            HienthiHNX =
                    IteratorHNX.getCurrentRow().getAttribute("HienThi").toString();
        }
        return HienthiHNX;
    }

    public int getMinHoseVnIndex() {
        DCIteratorBinding IteratorHose =
            Utils.getIteratorBinding("VnHastcIndexView1Iterator");
        if (IteratorHose.getAllRowsInRange().length > 0) {
            int length = IteratorHose.getAllRowsInRange().length;
            for (int i = 0; i < length; i++) {
                double chiso =
                    Double.valueOf(IteratorHose.getRowAtRangeIndex(i).getAttribute("ChiSo").toString());
                if (i == 0) {
                    chiso = Math.floor(chiso);
                    minHoseVnIndex = (int)chiso;
                }
                if (minHoseVnIndex > chiso) {
                    minHoseVnIndex = (int)chiso;
                }
            }
        }
        if (minHoseVnIndex > 10) {
            minHoseVnIndex = minHoseVnIndex - 10;
        }
        return minHoseVnIndex;
    }

    public int getMinHnxVnIndex() {
        DCIteratorBinding IteratorHnx =
            Utils.getIteratorBinding("VnHnxIndexView1Iterator");
        if (IteratorHnx.getAllRowsInRange().length > 0) {
            int length = IteratorHnx.getAllRowsInRange().length;
            for (int i = 0; i < length; i++) {
                double chiso =
                    Double.valueOf(IteratorHnx.getRowAtRangeIndex(i).getAttribute("ChiSo").toString());
                if (i == 0) {
                    chiso = Math.floor(chiso);
                    minHnxVnIndex = (int)chiso;
                }
                if (minHnxVnIndex > chiso) {
                    minHnxVnIndex = (int)chiso;
                }
            }
        }
        if (minHnxVnIndex > 1) {
            minHnxVnIndex = minHnxVnIndex - 1;
        }
        return minHnxVnIndex;
    }

    public int getMinHoseKLGD() {
        DCIteratorBinding IteratorHose =
            Utils.getIteratorBinding("VnHastcIndexView1Iterator");
        if (IteratorHose.getAllRowsInRange().length > 0) {
            int length = IteratorHose.getAllRowsInRange().length;
            for (int i = 0; i < length; i++) {
                double klgd =
                    Double.valueOf(IteratorHose.getRowAtRangeIndex(i).getAttribute("TongKlGdNumber").toString());
                if (i == 0) {
                    klgd = Math.floor(klgd);
                    minHoseKLGD = (int)klgd;
                }
                if (minHoseKLGD > klgd) {
                    minHoseKLGD = (int)klgd;
                }
            }
        }
        if (minHoseKLGD > 1) {
            minHoseKLGD = minHoseKLGD - 1;
        }
        return minHoseKLGD;
    }

    public int getMinHnxKLGD() {
        DCIteratorBinding IteratorHnx =
            Utils.getIteratorBinding("VnHnxIndexView1Iterator");
        if (IteratorHnx.getAllRowsInRange().length > 0) {
            int length = IteratorHnx.getAllRowsInRange().length;
            for (int i = 0; i < length; i++) {
                double klgd =
                    Double.valueOf(IteratorHnx.getRowAtRangeIndex(i).getAttribute("TongKlGdNumber").toString());
                if (i == 0) {
                    klgd = Math.floor(klgd);
                    minHnxKLGD = (int)klgd;
                }
                if (minHnxKLGD > klgd) {
                    minHnxKLGD = (int)klgd;
                }
            }
        }
        if (minHnxKLGD > 1) {
            minHnxKLGD = minHnxKLGD - 1;
        }
        return minHnxKLGD;
    }
}
